package net.homeip.jcperera.nu.core.config;

import java.io.Serializable;
import java.util.Set;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;

import net.homeip.jcperera.nu.core.CoreBase;
import net.homeip.jcperera.nu.core.msg.Logger;

import com.tangosol.net.CacheFactory;
import com.tangosol.net.NamedCache;

/**
 * <p>
 * Session Bean implementation class CoherenceCache, implements the Oracle Coherence cache interface. This can be used
 * if the framework is running in WebLogic and the Coherence framework has been installed into the domain during the
 * server installation.
 * </p>
 * 
 * <p>
 * Depends on the {@code nu.core.config.use.coherence.cache} system configuration property, whose allowed values are
 * {@code true} or {@code false}.
 * </p>
 * 
 * @name Coherence Cache Bean
 * @author julio.c.perera@gmail.com
 * @application nu
 * @since 0.1.0.0
 */

@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
@LocalBean
@ApplicationScoped
@Named(CoherenceCache.componentName)
public class CoherenceCache extends CoreBase implements Serializable
{
    // Required to implement Serializable interface
    private static final long     serialVersionUID = -1464117913795475154L;

    // CoreBase properties, for easy use from other components, as well as logging
    public static final int       runtimeLevel     = 0;
    public static final String    moduleName       = "core";
    public static final String    componentName    = "nu_core_coherence_cache_bean";

    // Get the log
    protected static final Logger log              = Logger.getLogger();

    protected NamedCache          nucache;
    private boolean               initialized      = false;

    public CoherenceCache()
    {
    }

    public void initialize()
    {
        log.info("Initializing Coherence Cache Service...");
        CacheFactory.ensureCluster();
        nucache = CacheFactory.getCache("nucache");
        initialized = true;
    }

    public String get(String propName)
    {
        if (!initialized) initialize();
        return (String) nucache.get(propName);
    }

    public Object put(String propName, String propValue)
    {
        if (!initialized) initialize();
        return nucache.put(propName, propValue);
    }

    public boolean containsKey(String propName)
    {
        if (!initialized) initialize();
        return nucache.containsKey(propName);
    }

    @SuppressWarnings("unchecked")
    public Set<String> keySet()
    {
        return (Set<String>) nucache.keySet();
    }

    protected void start()
    {
        log.info("Starting Coherence Cache Service...\n");
        if (!initialized) initialize();
    }

    protected void stop()
    {
        log.info("Stopping Coherence Cache Service...\n");
    }
}
